package com.hoow.shakoow.ai.network

import androidx.lifecycle.ViewModel
import com.hoow.shakoow.ai.data.*
import com.hoow.shakoow.shake.payBilling.PayProductUtil
import com.hoow.shakoow.shake.adManager.AdsIdUtil
import com.hoow.shakoow.utils.UserStateUtil
import kotlinx.coroutines.Dispatchers
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

object ApiAiReposity : ApiBaseReposity() {

    /**
     * app-获取APP更新版本
     *
     * @param reqAiBase body
     */
    fun getAppVersion(
        caller: ViewModel?,
        reqAiBase: ReqAiBase
    ) =
        fire(Dispatchers.IO, caller, reqAiBase.showLoading) {
            val response =
                ApiAiNetwork.getAiAppVersion(reqAiBase)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * app-获取APP配置
     *
     * @param reqAiBase body
     */
    fun getAppConfig(
        caller: ViewModel?,
        reqAiBase: ReqAiBase
    ) =
        fire(Dispatchers.IO, caller, reqAiBase.showLoading) {
            val response =
                ApiAiNetwork.getAppConfig(reqAiBase)
            if (response.code.toInt() == 0) {
                val obj = response.data
                // 保存id
                AdsIdUtil.mNatUnid = obj.natUnid ?: ""
                AdsIdUtil.mRewUnid = obj.rewUnid ?: ""
                PayProductUtil.mProUnid = obj.proUnid ?: ""
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 登录-广告id登录
     *
     * @param reqAiBase body
     */
    fun loginByAdvertId(caller: ViewModel?, reqAiBase: ReqAiBase) =
        fire(Dispatchers.IO, caller, reqAiBase.showLoading) {
            val response = ApiAiNetwork.loginByAdvertId(reqAiBase)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 用户-获取用户详情
     *
     * @param reqAiBase body
     */
    fun getUserInfo(
        caller: ViewModel?,
        reqAiBase: ReqAiBase
    ) =
        fire(Dispatchers.IO, caller, reqAiBase.showLoading) {
            val response =
                ApiAiNetwork.getUserInfo(reqAiBase)
            if (response.code.toInt() == 0) {
                val obj = response.data
                //保存用户状态
                UserStateUtil.mUserInfo = obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 用户-获取用户详情 根据id
     *
     * @param reqAiBase body
     */
    fun getUserInfoByUserId(
        caller: ViewModel?,
        reqAiBase: ReqAiBase
    ) =
        fire(Dispatchers.IO, caller, reqAiBase.showLoading) {
            val response =
                ApiAiNetwork.getUserInfoByUserId(reqAiBase)
            if (response.code.toInt() == 0) {
                val obj = response.data
                //保存用户状态
                UserStateUtil.mUserInfo = obj
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 工具-上传文件
     *
     * @param caller 参数
     * @param reqImageUpload body
     */
    fun uploadFile(
        caller: ViewModel?,
        reqImageUpload: ReqImageUpload
    ) =
        fire(Dispatchers.IO, caller, reqImageUpload.showLoading) {
            val map: HashMap<String, String> = HashMap()
            map["path"] = reqImageUpload.cardType

            val file = File(reqImageUpload.filePath)
            val requestBody =
                RequestBody.create(reqImageUpload.mediaType.toMediaTypeOrNull(), file)
            val bodyPart: MultipartBody.Part =
                MultipartBody.Part.createFormData("file", file.name, requestBody)

            val response =
                ApiAiNetwork.uploadFile(map, bodyPart)
            if (response.code.toInt() == 0) {
                val obj = response.data
//                obj.type = reqImageUpload.cardType
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 设置用户昵称与性别
     *
     * @param reqSetUserName body
     */
    fun setUserName(
        caller: ViewModel?,
        reqSetUserName: ReqSetUserName
    ) =
        fire(Dispatchers.IO, caller, reqSetUserName.showLoading) {
            val response =
                ApiAiNetwork.setUserName(reqSetUserName)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 设置用户头像
     *
     * @param reqSetUserHeadImg body
     */
    fun setUserHeadImg(
        caller: ViewModel?,
        reqSetUserHeadImg: ReqSetUserHeadImg
    ) =
        fire(Dispatchers.IO, caller, reqSetUserHeadImg.showLoading) {
            val response =
                ApiAiNetwork.setUserHeadImg(reqSetUserHeadImg)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 开始摇一摇
     *
     * @param reqAiBase body
     */
    fun startShake(
        caller: ViewModel?,
        reqAiBase: ReqAiBase
    ) =
        fire(Dispatchers.IO, caller, reqAiBase.showLoading) {
            val response =
                ApiAiNetwork.startShake(reqAiBase)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else if (response.code.toInt() == 500) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 摇一摇返回的用户
     *
     * @param reqGetShakeUserInfo body
     */
    fun getShakeUserInfo(
        caller: ViewModel?,
        reqGetShakeUserInfo: ReqGetShakeUserInfo
    ) =
        fire(Dispatchers.IO, caller, reqGetShakeUserInfo.showLoading) {
            val response =
                ApiAiNetwork.getShakeUserInfo(reqGetShakeUserInfo)
            val obj = response.data
            // 这个接口默认返回成功
            Result.success(obj)
//            if (response.code.toInt() == 0) {
//                Result.success(obj)
//            } else {
//                Result.failure(NetworkException(response.code, response.msg))
//            }
        }

    /**
     * 设置用户签名
     *
     * @param reqSetUserSignature body
     */
    fun setUserSignature(
        caller: ViewModel?,
        reqSetUserSignature: ReqSetUserSignature
    ) =
        fire(Dispatchers.IO, caller, reqSetUserSignature.showLoading) {
            val response =
                ApiAiNetwork.setUserSignature(reqSetUserSignature)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 获取用户相册
     *
     * @param reqAiBase body
     */
    fun getUserPhotos(
        caller: ViewModel?,
        reqAiBase: ReqAiBase
    ) =
        fire(Dispatchers.IO, caller, reqAiBase.showLoading) {
            val response =
                ApiAiNetwork.getUserPhotos(reqAiBase)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 上传用户相册
     *
     * @param reqUploadUserPhotos body
     */
    fun uploadUserPhotos(
        caller: ViewModel?,
        reqUploadUserPhotos: ReqUploadUserPhotos
    ) =
        fire(Dispatchers.IO, caller, reqUploadUserPhotos.showLoading) {
            val response =
                ApiAiNetwork.uploadUserPhotos(reqUploadUserPhotos)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 删除用户相册
     *
     * @param reqDeleteUserPhotos body
     */
    fun deleteUserPhotos(
        caller: ViewModel?,
        reqDeleteUserPhotos: ReqDeleteUserPhotos
    ) =
        fire(Dispatchers.IO, caller, reqDeleteUserPhotos.showLoading) {
            val response =
                ApiAiNetwork.deleteUserPhotos(reqDeleteUserPhotos)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 获取打招呼的历史
     *
     * @param reqAiBase body
     */
    fun getGreetHistoryList(
        caller: ViewModel?,
        reqAiBase: ReqAiBase
    ) =
        fire(Dispatchers.IO, caller, reqAiBase.showLoading) {
            val response =
                ApiAiNetwork.getGreetHistoryList(reqAiBase)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * shake-获取历史列表
     *
     * @param reqAiBase body
     */
    fun getShakeHistoryList(
        caller: ViewModel?,
        reqAiBase: ReqAiBase
    ) =
        fire(Dispatchers.IO, caller, reqAiBase.showLoading) {
            val response =
                ApiAiNetwork.getShakeHistoryList(reqAiBase)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 获取对话详情
     *
     * @param reqChatInfo body
     */
    fun getChatInfo(
        caller: ViewModel?,
        reqChatInfo: ReqChatInfo
    ) =
        fire(Dispatchers.IO, caller, reqChatInfo.showLoading) {
            val response =
                ApiAiNetwork.getChatInfo(reqChatInfo)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 打招呼
     *
     * @param reqToGreet body
     */
    fun toGreet(
        caller: ViewModel?,
        reqToGreet: ReqToGreet
    ) =
        fire(Dispatchers.IO, caller, reqToGreet.showLoading) {
            val response =
                ApiAiNetwork.toGreet(reqToGreet)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 回复消息
     *
     * @param reqToReply body
     */
    fun toReply(
        caller: ViewModel?,
        reqToReply: ReqToReply
    ) =
        fire(Dispatchers.IO, caller, reqToReply.showLoading) {
            val response =
                ApiAiNetwork.toReply(reqToReply)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 记录看完广告
     *
     * @param reqLookedAdvert body
     */
    fun saveLookedAdvert(
        caller: ViewModel?,
        reqLookedAdvert: ReqLookedAdvert
    ) =
        fire(Dispatchers.IO, caller, reqLookedAdvert.showLoading) {
            val response =
                ApiAiNetwork.saveLookedAdvert(reqLookedAdvert)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 获取用户vip信息
     *
     * @param reqAiBase body
     */
    fun getUserVipInfo(
        caller: ViewModel?,
        reqAiBase: ReqAiBase
    ) =
        fire(Dispatchers.IO, caller, reqAiBase.showLoading) {
            val response =
                ApiAiNetwork.getUserVipInfo(reqAiBase)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 获取商品id
     *
     * @param reqAiBase body
     */
    fun getPayOrderId(
        caller: ViewModel?,
        reqAiBase: ReqAiBase
    ) =
        fire(Dispatchers.IO, caller, reqAiBase.showLoading) {
            val response =
                ApiAiNetwork.getPayOrderId(reqAiBase)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 保存订单支付信息
     *
     * @param reqSavePayOrderInfo body
     */
    fun savePayOrderInfo(
        caller: ViewModel?,
        reqSavePayOrderInfo: ReqSavePayOrderInfo
    ) =
        fire(Dispatchers.IO, caller, reqSavePayOrderInfo.showLoading) {
            val response =
                ApiAiNetwork.savePayOrderInfo(reqSavePayOrderInfo)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 订单支付成功
     *
     * @param reqSavePayOrderInfo body
     */
    fun payOrderInfoSuccess(
        caller: ViewModel?,
        reqSavePayOrderInfo: ReqSavePayOrderInfo
    ) =
        fire(Dispatchers.IO, caller, reqSavePayOrderInfo.showLoading) {
            val response =
                ApiAiNetwork.payOrderInfoSuccess(reqSavePayOrderInfo)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 消息-删除招呼记录
     *
     * @param reqDeleteGreetRecord body
     */
    fun deleteGreetRecord(
        caller: ViewModel?,
        reqDeleteGreetRecord: ReqDeleteGreetRecord
    ) =
        fire(Dispatchers.IO, caller, reqDeleteGreetRecord.showLoading) {
            val response =
                ApiAiNetwork.deleteGreetRecord(reqDeleteGreetRecord)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 消息-删除摇一摇记录
     *
     * @param reqDeleteShakeRecord body
     */
    fun deleteShakeRecord(
        caller: ViewModel?,
        reqDeleteShakeRecord: ReqDeleteShakeRecord
    ) =
        fire(Dispatchers.IO, caller, reqDeleteShakeRecord.showLoading) {
            val response =
                ApiAiNetwork.deleteShakeRecord(reqDeleteShakeRecord)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 消息-阅读打招呼消息
     *
     * @param reqReadGreetMsg body
     */
    fun readGreetMsg(
        caller: ViewModel?,
        reqReadGreetMsg: ReqReadGreetMsg
    ) =
        fire(Dispatchers.IO, caller, reqReadGreetMsg.showLoading) {
            val response =
                ApiAiNetwork.readGreetMsg(reqReadGreetMsg)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 消息-获取用户是否有新消息
     *
     * @param reqAiBase body
     */
    fun getLatestGreetMsg(
        caller: ViewModel?,
        reqAiBase: ReqAiBase
    ) =
        fire(Dispatchers.IO, caller, reqAiBase.showLoading) {
            val response =
                ApiAiNetwork.getLatestGreetMsg(reqAiBase)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }

    /**
     * 提交用户反馈
     *
     * @param reqSubmitFeedback body
     */
    fun submitFeedback(
        caller: ViewModel?,
        reqSubmitFeedback: ReqSubmitFeedback
    ) =
        fire(Dispatchers.IO, caller, reqSubmitFeedback.showLoading) {
            val response =
                ApiAiNetwork.submitFeedback(reqSubmitFeedback)
            if (response.code.toInt() == 0) {
                val obj = response.data
                Result.success(obj)
            } else {
                Result.failure(NetworkException(response.code, response.msg))
            }
        }
}